package com.hainan.cs.dao;

import com.hainan.cs.bean.StudentBean;
import com.hainan.cs.bean.UserBean;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.util.List;

public class StudentDaoImp implements StudentDao {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public StudentBean getStudent(String id) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        StudentBean user = (StudentBean) session.get(StudentBean.class, id);
        session.close();
        return user;
    }

    @Override
    public void insert(StudentBean studentBean) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(studentBean);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public void delete(String id) {
        Session session=sessionFactory.openSession();
        session.beginTransaction();
        StudentBean studentBean=(StudentBean) session.get(StudentBean.class, id);
        session.delete(studentBean);
        session.getTransaction().commit();
        session.close();
    }

    @Override
    public List<StudentBean> queryStudent(String stuName) {
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        String str = "from StudentBean student where username='" + stuName + "'";
        Query query = session.createQuery(str);
        List<StudentBean> userlist = query.list();
        session.close();
        return userlist;
    }
}
